<?php
class Category extends Model {
	public function getCategoryByPid($pid,$start=0,$limit=10) {
		$result = $this->dbo->query("SELECT category_id,name,description,status,parent_id FROM category WHERE parent_id='".(int)$pid."' LIMIT $start,$limit");
		return $result->rows;
	}
	public function getCategoryById($id) {
		$result = $this->dbo->query("SELECT * FROM category WHERE category_id='".(int)$id."' LIMIT 1");
		return $result->row;
	}
	public function addCategory($data) {
		return $this->dbo->query("INSERT INTO category (name,parent_id,description,sort_order,date_added,date_modified,status) VALUES('".$this->dbo->escape($data['name'])."','".(int)$this->dbo->escape($data['parent_id'])."','".$this->dbo->escape($data['description'])."','".(int)$this->dbo->escape($data['sort_order'])."','".$this->dbo->escape($data['date_added'])."','".$this->dbo->escape($data['date_modified'])."','".(int)$this->dbo->escape($data['status'])."')");
	}
	public function updateCategory($data,$category_id) {
		return $this->dbo->query("UPDATE category SET name='".$this->dbo->escape($data['name'])."',parent_id='".(int)$this->dbo->escape($data['parent_id'])."',description='".$this->dbo->escape($data['description'])."',sort_order='".(int)$this->dbo->escape($data['sort_order'])."',date_added='".$this->dbo->escape($data['date_added'])."',date_modified='".$this->dbo->escape($data['date_modified'])."',status='".(int)$this->dbo->escape($data['status'])."' WHERE category_id = '".(int)$category_id."'");
	}
	public function delCategory($ids) {
		return $this->dbo->query("DELETE FROM category WHERE category_id IN ($ids)");
	}
	public function countCategories() {
		$result = $this->dbo->query("SELECT count(*) as total FROM category WHERE 1");
		return $result->row['total'];
	}
	public function getAllCategories() {
		$result = $this->dbo->query("SELECT category_id,name,parent_id FROM category");
		return $result->rows;
	}
	public function getCatSelect($def_cat='') {
		$data_all = $this->getAllCategories();
		$Tree = $this->getTreeObject();
		$Tree->set_data_cache($data_all);
		$Tree->sort_data(0, 1);
		$cat_arr = $Tree->get_sort_array();
		
		$cat_sel = create_select($cat_arr, '0', '1', $def_cat);
		return $cat_sel;
	}
	public function getTreeObject() {
		$Tree = new Tree();
		$Tree->key_id = 'category_id';
		$Tree->key_name = 'name';
		$Tree->key_parent = 'parent_id';
		return $Tree;
	}
}
?>